package com.piece.core.mongo.service;

import com.piece.core.mongo.repository.MongoJobLogRepository;
import com.piece.core.mongo.entity.MongoJobLog;
import com.piece.core.framework.support.bootstrap.DataTable;
import com.piece.core.framework.support.bootstrap.QueryTable;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;

@Transactional
@Service(value = "mongoJobLogService")
public class MongoJobLogService {

    @Autowired
    private MongoJobLogRepository mongoJobLogRepository;

    public MongoJobLog insert(MongoJobLog log) {
        return mongoJobLogRepository.save(log);
    }

    public MongoJobLog update(MongoJobLog log) {
        return mongoJobLogRepository.saveOrUpdate(log.getLogId(), log);
    }

    public void delete(String logId) {
        MongoJobLog log = new MongoJobLog();
        log.setLogId(logId);
        mongoJobLogRepository.delete(log);
    }

    public void deleteAll() {
        Query query = new Query();
        query.addCriteria(Criteria.where("status").ne("2"));
        mongoJobLogRepository.deleteByQuery(query);
    }

    public MongoJobLog findOne(String logId) {
        return mongoJobLogRepository.get(logId);
    }

    public List<MongoJobLog> findList(MongoJobLog log) {
        return mongoJobLogRepository.findByCondition(log);
    }

    public DataTable findPage(QueryTable queryTable) {
        return mongoJobLogRepository.findPage(queryTable);
    }
}